﻿using System;
using System.Web.Mvc;
using Framework.Poco;
using Shell.DomainDot;
using Framework.Domain;

namespace Framework.Mvc.Controllers
{
    /// <summary>
    /// 经销商分组控制器
    /// </summary>
    public class AgencyGroupController : Controller
    {
        private IDomainDotFactory factory = DomainDotFactory.Factory;

        /// <summary>
        /// 经销商分组创建时信息
        /// </summary>
        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// 提交经销商分组创建信息
        /// </summary>
        [HttpPost]
        public ActionResult Create(AgencyGroup instance)
        {
            var agencyGroupContext = this.factory.Create<GlobalContext>();
            if (String.IsNullOrEmpty(instance.Name))
            {
                this.ModelState.AddModelError("instance.Name", "名称不能为空!");
                return View();
            }
            if (agencyGroupContext.HasSameAgencyGroup(instance.Name))
            {
                this.ModelState.AddModelError("instance.Name", "具有相同的经销商分组名称");
                return View();
            }
            agencyGroupContext.CreateAgencyGroup(instance);
            return this.RedirectToAction("List");
        }

        /// <summary>
        /// 获得删除经销商分组时的信息
        /// </summary>
        [HttpGet]
        public ActionResult Delete(Guid identity)
        {
            var agencyGroupContext = factory.Create<AgencyGroupContext>();
            var model = new AgencyGroupDeleteViewModel();
            model.Instance = agencyGroupContext.Find(identity);
            return View(model);
        }

        /// <summary>
        /// 提交需要删除的经销商分组
        /// </summary>
        [HttpPost]
        public ActionResult Delete(AgencyGroup instance)
        {
            var agencyGroupContext = factory.Create<AgencyGroupContext>();
            agencyGroupContext.Delete(instance);
            return this.RedirectToAction("List");
        }

        /// <summary>
        /// 获得需要修改的经销商分组信息
        /// </summary>
        [HttpGet]
        public ActionResult Edit(Guid identity)
        {
            var model = new AgencyGroupEditViewModel();
            var agencyGroupContext = factory.Create<AgencyGroupContext>();
            model.Instance = agencyGroupContext.Find(identity);
            return View(model);
        }

        /// <summary>
        /// 提交修改经销商分组时的信息
        /// </summary>
        [HttpPost]
        public ActionResult Edit(AgencyGroup instance)
        {
            var agencyGroupContext = factory.Create<AgencyGroupContext>();
            var globalContext = factory.Create<GlobalContext>();

            if (String.IsNullOrEmpty(instance.Name))
            {
                this.ModelState.AddModelError("instance.Name", "分组名称不能为空!");
                //TODO ViewModel获得经销商分组名称时错误
                var model = new AgencyGroupEditViewModel();
                model.Instance = instance;
                return View(model);
            }
            if (globalContext.HasSameAgencyGroup(instance.Name))
            {
                this.ModelState.AddModelError("instance.Name", "具有相同的经销商分组名称");
                //TODO ViewModel获得经销商分组名称时错误
                var model = new AgencyGroupEditViewModel();
                model.Instance = instance;
                return View(model);
            }
            agencyGroupContext.Edit(instance);
            return this.RedirectToAction("List");
        }

        /// <summary>
        /// 经销商分组列表
        /// </summary>
        public ActionResult List(int currentPage, int pageSize)
        {
            var agencyGroupContext = factory.Create<GlobalContext>();
            var model = new AgencyGroupListViewModel();
            model.List = agencyGroupContext.GetAllAgencyGroups();
            return View(model);
        }
    }
}